package development.app;

import iplayparticle.view.iplayparticleResources;

import java.util.Vector;

import state.Clip;
import state.DrawListener;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Sprite;
import state.TimerListener;
import state.component.GameScrollBar;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.socket.MapDataPackage;
import state.util.GameBoolean;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.NumberUtil;
import state.util.array.ByteArray;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.FrmUserInfo;
import development.Global;
import development.app.football.FrmBettingMenu;
import development.bean.Category;
import development.bean.User;
import development.group.FrmGroupBongDaXoSo;
import development.group.FrmGroupMenu;
import development.group.FrmGroupNapXuAndExchangeGold;
import development.group.FrmGroupThoiTietTinTuc;

public class FrmDauGiaNguoc extends GameForm implements TimerListener {
  public static final int DAU_GIA_NGUOC_PHASE_LIST_REQUEST = 800;
  public static final int DAU_GIA_NGUOC_PHASE_LIST_RESPONSE = 801;
  public static final int DAU_GIA_NGUOC_VOTING_OBJECT_REQUEST = 802;
  public static final int DAU_GIA_NGUOC_VOTING_OBJECT_RESPONSE = 803;
  public static final int DAU_GIA_NGUOC_DAT_GIA_REQUEST = 804;
  public static final int DAU_GIA_NGUOC_DAT_GIA_RESPONSE = 805;
  public static final int DAU_GIA_NGUOC_USERS_REQUEST = 806;
  public static final int DAU_GIA_NGUOC_USERS_RESPONSE = 807;

  private Hashtable parameters;
  private ImageText text8;
  private ImageText text11;
  private ImageText text13;
  private int categoryIndex;
  private Vector categories;
  private String type;
  private Hashtable logAction = null;
  private int currentAction;
  private final int DEFAULT_TEXT_X = 5;
  private GameScrollBar scrollBar;
  private Clip textView; // Là khung nhìn để vẽ đoạn text ra
  private int dy;
  private int TEMP_Y;

  // Thoi gian dem nguoc
  private int hours;
  private int minutes;
  private int seconds;
  private TextField txtHours;
  private TextField txtMinutes;
  private TextField txtSeconds;
  private TextField txtMoney;
  private Sprite betButtonSprite;
  private GameImage buttonImage;

  private boolean isRequestVotingObject = false;
  public static GameForm backTab;

  private final String WAITING_MESSAGE = "Xin chờ trong giây lát...";

  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmDauGiaNguoc();
    }
    return singleton;
  }

  public void init(Hashtable parameters1) {
    this.parameters = parameters1;
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    text11 = FontManager.getFont(FontManager.FONT_SIZE_11);
    text11.setBold(true);
    text13 = FontManager.getFont(FontManager.FONT_SIZE_13);
    text13.setBold(true);
    type = "phase";
    currentAction = 1;
    logAction = new Hashtable();
    categories = new Vector();

    dy = DrawUtils.getTopBarHeight();
    TEMP_Y = 50;
    textView = new Clip(DEFAULT_TEXT_X, dy, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT - dy - 10); // Là khung nhìn để vẽ đoạn text ra
    scrollBar = new GameScrollBar(Constants.SCREEN_WIDTH - 5, 25, 5, Constants.SCREEN_HEIGHT - 50, textView, _manager);

    txtHours = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
    txtHours.setSize(30, text13.getHeight());
    txtHours.setFont(text13);
    txtHours.setAnchor(GameConstants.TOP_LEFT_ANCHOR);

    txtMinutes = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
    txtMinutes.setSize(30, text13.getHeight());
    txtMinutes.setFont(text13);
    txtMinutes.setAnchor(GameConstants.TOP_LEFT_ANCHOR);

    txtSeconds = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
    txtSeconds.setSize(30, text13.getHeight());
    txtSeconds.setFont(text13);
    txtSeconds.setAnchor(GameConstants.TOP_LEFT_ANCHOR);

    txtMoney = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
    txtMoney.setSize(GameConstants.SCREEN_WIDTH / 2 + 7, text8.getHeight());
    txtMoney.setAnchor(GameConstants.TOP_LEFT_ANCHOR);

    txtHours.setVisible(false);
    txtMinutes.setVisible(false);
    txtSeconds.setVisible(false);
    txtMoney.setVisible(false);

    buttonImage = new GameImage(iplayparticleResources.image_button, null, null);

    betButtonSprite = new Sprite(null, _manager, 0, 0, Constants.TOP_LEFT_ANCHOR);
    betButtonSprite.setImage(buttonImage);
    betButtonSprite.setAnchor(Constants.TOP_LEFT_ANCHOR);
    betButtonSprite.setVisible(false);
    betButtonSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        text8.drawString2(g, "Đặt giá", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
      }
    });
    setTimerListener(this);
    if (parameters1 != null) {
      GameBoolean requestVotingObject = (GameBoolean) parameters1.get("requestVotingObject");
      if (requestVotingObject != null) {
        isRequestVotingObject = requestVotingObject.booleanValue();
      }
    }
    if (isRequestVotingObject) {
      Vector param = new Vector();
      param.addElement(parameters1.get("phaseId"));
      request(DAU_GIA_NGUOC_VOTING_OBJECT_REQUEST, param);
    }
    if (!isRequestVotingObject) {
      request(DAU_GIA_NGUOC_PHASE_LIST_REQUEST, new Vector());
      clearCategory();
    }
    _manager.enableTouchPad();
    DrawUtils.setBottomBar(_manager, "Menu chính", null, "Trở về");
  }

  public void draw(Graphics g) {
    DrawUtils.drawAppBackground(g);
    if (categories.isEmpty()) {
      text8.drawString2(g, WAITING_MESSAGE, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.TOP_HCENTER_ANCHOR);
    }
    if (type.equals("phase")) {
      drawPhase(g);
    }
    if (type.equals("voting")) {
      drawVoting(g);
    }
    if (type.equals("listUsers")) {
      drawListUsers(g);
    }
    drawMenu(g);
  }

  private void drawMenu(Graphics g) {
    if (type.equals("voting")) {
      DrawUtils.getBottomBar(_manager).setCenterLabel("Đặt giá");
      if (txtMoney.isFocused()) {
        DrawUtils.getBottomBar(_manager).setRightLabel("Xóa");
      } else {
        DrawUtils.getBottomBar(_manager).setRightLabel("Trở về");
      }
    }
    if (type.equals("phase") || type.equals("listUsers")) {
      DrawUtils.getBottomBar(_manager).setCenterLabel(null);
      DrawUtils.getBottomBar(_manager).setRightLabel("Trở về");
    }
  }

  private void drawListUsers(Graphics g) {
    int textY = dy;
    int count = 1;
    for (int i = 0; i < categories.size(); i++) {
      Category category = (Category) categories.elementAt(i);
      text11.drawString2(g, String.valueOf(count) + ". ", GameColor.YELLOW_CODE, 5, textY, GameConstants.TOP_LEFT_ANCHOR);
      text11.drawString2(g, "Tài khoản: " + category.code, GameColor.YELLOW_CODE, 20, textY, GameConstants.TOP_LEFT_ANCHOR);
      textY = textY + 20;
      text11.drawString2(g, "Số điện thoại: " + category.name, GameColor.YELLOW_CODE, 20, textY, GameConstants.TOP_LEFT_ANCHOR);
      textY = textY + 20;
      g.setColor(0xC0C0C0);
      g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
      count++;
    }
  }

  private void drawVoting(Graphics g) {
    int textY = dy;
    for (int i = 0; i < categories.size(); i++) {
      Category category = (Category) categories.elementAt(i);
      g.setColor(0xA9A9A9);
      g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, text11.getHeight());
      g.drawRect(0, textY, GameConstants.SCREEN_WIDTH, text11.getHeight());
      text11.drawString2(g, "Sản phẩm đấu giá", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, textY, GameConstants.TOP_HCENTER_ANCHOR);
      textY = textY + 20;
      g.drawImage(category.image, 0, textY, GameConstants.TOP_LEFT_ANCHOR);
      text11.drawString3(g, category.name, GameColor.YELLOW_CODE, category.image.getWidth() + 10, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      text8.drawString3(g, "Giá thị trường: " + category.exParam.elementAt(1) + " VND", GameColor.YELLOW_CODE, category.image.getWidth() + 10, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      text8.drawString3(g, "Thời gian còn lại:", GameColor.YELLOW_CODE, category.image.getWidth() + 10, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      txtHours.setPosition(category.image.getWidth() + 10, textY);
      txtMinutes.setPosition(txtHours.getX() + txtHours.getWidth() + 2, textY);
      txtSeconds.setPosition(txtMinutes.getX() + txtMinutes.getWidth() + 2, textY);
      txtHours.draw(g);
      txtMinutes.draw(g);
      txtSeconds.draw(g);
      textY = textY + 30;
      text8.drawString3(g, "Top 3 tài khoản & SĐT >>", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 5, textY, GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
      if (categoryIndex == 1) {
        g.setColor(GameColor.WHITE_CODE);
        g.drawLine(GameConstants.SCREEN_WIDTH - text8.stringWidth("Top 3 tài khoản & SĐT >>") - 5, textY + 17, GameConstants.SCREEN_WIDTH - 7, textY + 17);
      }
      textY = textY + 20;
      text8.drawString3(g, "Cập nhật thông tin tài khoản >>", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 5, textY, GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
      if (categoryIndex == 2) {
        g.setColor(GameColor.WHITE_CODE);
        g.drawLine(GameConstants.SCREEN_WIDTH - text8.stringWidth("Cập nhật thông tin tài khoản >>") - 5, textY + 17, GameConstants.SCREEN_WIDTH - 7, textY + 17);
      }
      textY = textY + 20;
      g.setColor(0xA9A9A9);
      g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, text11.getHeight());
      g.drawRect(0, textY, GameConstants.SCREEN_WIDTH, text11.getHeight());
      text11.drawString2(g, "Đặt giá sản phẩm", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, textY, GameConstants.TOP_HCENTER_ANCHOR);
      textY = textY + 30;
      text8.drawString2(g, "Số $: ", GameColor.WHITE_CODE, 5, textY, GameConstants.TOP_LEFT_ANCHOR);
      txtMoney.setPosition(text8.stringWidth("Số $: ") + 2, textY);
      txtMoney.draw(g);
      betButtonSprite.setPosition(txtMoney.getRealX() + txtMoney.getWidth() + 5, txtMoney.getRealY() - 3);
      if (categoryIndex == 3) {
        txtMoney.setFocused(true);
      } else {
        txtMoney.setFocused(false);
      }
      textY = textY + 30;
      text8.drawParagraph(g, "Lưu ý: Để tham gia đấu giá, bạn phải điền đầy đủ thông tin tài khoản.", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, 5, textY);
    }
  }

  private void drawPhase(Graphics g) {
    int textY = dy;
    int index = 0;
    for (int i = 0; i < categories.size(); i++) {
      Category category = (Category) categories.elementAt(i);
      if (categoryIndex == index) {
        g.setColor(0x53A4CF);
        g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, text8.getHeight() + 80);
      }
      g.drawImage(category.image, 0, textY, GameConstants.TOP_LEFT_ANCHOR);
      text8.drawString3(g, category.name, GameColor.YELLOW_CODE, category.image.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      text8.drawString3(g, "Thời gian đấu giá:", GameColor.YELLOW_CODE, category.image.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      text8.drawString3(g, (String) category.exParam.elementAt(0) + " đến " + (String) category.exParam.elementAt(1), GameColor.YELLOW_CODE, category.image.getWidth() + 5, textY,
          GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      if (category.fee > 0) {
        text8.drawString3(g, "Phí chơi: " + category.fee + "$", GameColor.YELLOW_CODE, category.image.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
      }
      String status = (String) category.exParam.elementAt(category.exParam.size() - 1);
      if (status.equals("1")) {
        text8.drawString3(g, "Đang diễn ra >>", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 5, textY, GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
      }else{
        text8.drawString3(g, "Đã kết thúc", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - 5, textY, GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
      }
      textY = textY + 20;
      g.setColor(0xC0C0C0);
      g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
      index++;
    }
  }

  public void onRecieveData(DataPackage dataPackage) {
    int header = dataPackage.getHeader();
    MapDataPackage mapDataPackage = MapDataPackage.getMapPackage(dataPackage);
    type = mapDataPackage.popString("type");
    if (type.equals("-1")) {
      Global.alert.showAlert2(this, GameAlert.OK_TYPE, "Chưa có dữ liệu cho mục này", -1);
      return;
    }
    Category category = null;
    GameGlobal.systemCanvas.hideAlert();
    switch (header) {
    case DAU_GIA_NGUOC_PHASE_LIST_RESPONSE:
      category = new Category();
      category.id = mapDataPackage.getString("id");
      category.code = mapDataPackage.getString("code");
      category.name = mapDataPackage.getString("name");
      ByteArray btImage = mapDataPackage.getByteArray("avatarImage");
      category.image = ImageUtil.resizeImage2(GameImage.createImage(btImage, 0, btImage.len()),50,50);
      category.fee = mapDataPackage.getInt("fee");
      category.exParam.addElement(mapDataPackage.getString("startTime"));
      category.exParam.addElement(mapDataPackage.getString("endTime"));
      //category.exParam.addElement(mapDataPackage.getString("votingObjectId"));
      category.exParam.addElement(mapDataPackage.getString("status"));
      categories.addElement(category);
      TEMP_Y = category.image.getHeight();
      txtHours.setVisible(false);
      txtMinutes.setVisible(false);
      txtSeconds.setVisible(false);
      txtMoney.setVisible(false);
      betButtonSprite.setVisible(false);
      break;
    case DAU_GIA_NGUOC_VOTING_OBJECT_RESPONSE:
      category = new Category();
      category.id = mapDataPackage.getString("votingObjectId");
      category.code = mapDataPackage.getString("code");
      category.name = mapDataPackage.getString("name");
      ByteArray btPreviewImage = mapDataPackage.getByteArray("previewImage");
      category.image = ImageUtil.resizeImage2(GameImage.createImage(btPreviewImage, 0, btPreviewImage.len()),50,50);
      category.exParam.addElement(mapDataPackage.getString("money"));
      category.exParam.addElement(mapDataPackage.getString("phaseId"));
      category.exParam.addElement(mapDataPackage.getString("fee"));
      hours = mapDataPackage.getLong("hours");
      minutes = mapDataPackage.getLong("minutes") * 10;
      seconds = mapDataPackage.getLong("seconds") * 10;
      txtHours.setVisible(true);
      txtMinutes.setVisible(true);
      txtSeconds.setVisible(true);
      txtMoney.setVisible(true);
      betButtonSprite.setVisible(true);
      txtMoney.clearText();
      categories.addElement(category);
      break;
    case DAU_GIA_NGUOC_DAT_GIA_RESPONSE:
      String message = mapDataPackage.getString("message");
      StringArray arrMessage = GameStringUtils.split(message, "@@@");
      Global.alert.showAlert3(this, GameAlert.OK_TYPE, arrMessage);
      break;
    case DAU_GIA_NGUOC_USERS_RESPONSE:
      category = new Category();
      category.id = mapDataPackage.getString("phaseId");
      category.code = mapDataPackage.getString("username");
      category.name = mapDataPackage.getString("mobile");
      categories.addElement(category);
      txtHours.setVisible(false);
      txtMinutes.setVisible(false);
      txtSeconds.setVisible(false);
      txtMoney.setVisible(false);
      betButtonSprite.setVisible(false);
      break;
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.FIRE:
      if (type.equals("phase")) {
        Category category = (Category) categories.elementAt(categoryIndex);
        if (isInvalidUserMoney(category.fee)) {
          return;
        }
        Hashtable parameters1 = new Hashtable();
        parameters1.put("RequestType", "DAU_GIA_NGUOC");
        parameters1.put("phaseId", category.id);
        parameters1.put("status", category.exParam.elementAt(category.exParam.size() - 1));
        if (parentGroup instanceof FrmGroupBongDaXoSo) {
          FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
          frmGroupBongDaXoSo.getMultiTab().setTab(2, FrmBettingMenu.instance(), parameters1);
        } else if (parentGroup instanceof  FrmGroupThoiTietTinTuc) {
          FrmGroupThoiTietTinTuc frmGroupThoiTietTinTuc = (FrmGroupThoiTietTinTuc) FrmGroupThoiTietTinTuc.instance();
          frmGroupThoiTietTinTuc.getMultiTab().setTab(2, FrmBettingMenu.instance(), parameters1);
        }
        return;
      }
      if (type.equals("voting")) {
        Category category = (Category) categories.elementAt(0);
        if (categoryIndex == 1) {
          currentAction++;
          Vector value = new Vector();
          value.addElement(category.exParam.elementAt(1));
          request(DAU_GIA_NGUOC_USERS_REQUEST, value);
          txtHours.setVisible(false);
          txtMinutes.setVisible(false);
          txtSeconds.setVisible(false);
          txtMoney.setVisible(false);
          betButtonSprite.setVisible(false);
          clearCategory();
        } else if (categoryIndex == 2) {
          FrmUserInfo.goBackForm = this;
          Global.nextState(FrmUserInfo.instance(), null);
        } else if (categoryIndex == 4) {
          if (txtMoney.getText().equals("")) {
            Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa đặt giá sản phẩm");
            return;
          }
          
          int betMoney = NumberUtil.parseInt(txtMoney.getText());
          if (betMoney == NumberUtil.ERROR_VALUE) {
            Global.alert.showAlert(this, GameAlert.OK_TYPE, "Số $ phải là số");
            return;
          }
          if (betMoney == 0) {
            Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa đặt giá sản phẩm");
            return;
          }
          if (!validateUserInfo()) {
            Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create1( "Thông tin TK của bạn chưa đủ", "Bạn có muốn cập nhật không?" ), 100);
            return;
          }
          Vector value = new Vector();
          // phaseId
          value.addElement(category.exParam.elementAt(1));
          // votingObjectId
          value.addElement(category.id);
          // tien dat gia
          value.addElement(String.valueOf(betMoney));
          // phi choi
          value.addElement(category.exParam.elementAt(2));
          request(DAU_GIA_NGUOC_DAT_GIA_REQUEST, value);
          Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(10000);
        }
        if (txtMoney.isFocused()) {
          txtMoney.pointerReleased(txtMoney, txtMoney.getRealX(), txtMoney.getRealY());
        }
      }
      break;
    case Key.DOWN:
      if (!categories.isEmpty()) {
        if (type.equals("phase")) {
          if (categoryIndex < categories.size() - 1) {
            categoryIndex++;
          }
        }
      }
      if (type.equals("phase")) {
        if(scrollBar.canScrollUp()){
          dy = dy - TEMP_Y;
        }
      }
      if (type.equals("voting")) {
        if (categoryIndex < 4) {
          categoryIndex++;
        }
        if (categoryIndex == 4) {
          betButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Đặt giá", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        } else {
          betButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Đặt giá", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (!Constants.IS_240x320_SCREEN) {
          if (dy > DrawUtils.getBottomBarHeight() - 100) {
            dy -= 5;
          }
        }
      }
      break;
    case Key.UP:
      if (categoryIndex > 0) {
        categoryIndex--;
      }
      if (type.equals("phase")) {
        if(scrollBar.canScrollDown()){
          dy = dy + TEMP_Y;
        }
        else{
          dy = DrawUtils.getTopBarHeight();
        }
      }
      if (type.equals("voting")) {
        if (categoryIndex == 4) {
          betButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Đặt giá", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        } else {
          betButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Đặt giá", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (!Constants.IS_240x320_SCREEN) {
          if (dy < DrawUtils.getTopBarHeight()) {
            dy += 5;
          }
        }
      }
      break;
    case Key.SOFT_LEFT:
      Global.nextState(FrmGroupMenu.instance(), null);
      break;
    case Key.SOFT_RIGHT:
      if (type.equals("voting")) {
        if (txtMoney.isFocused()) {
          txtMoney.deleteLastChar();
        } else {
          txtHours.setVisible(false);
          txtMinutes.setVisible(false);
          txtSeconds.setVisible(false);
          txtMoney.setVisible(false);
          betButtonSprite.setVisible(false);
          
          if (parentGroup instanceof FrmGroupBongDaXoSo) {
            FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
            frmGroupBongDaXoSo.getMultiTab().setTab(2, backTab, parameters);
          } else if (parentGroup instanceof  FrmGroupThoiTietTinTuc) {
            FrmGroupThoiTietTinTuc frmGroupThoiTietTinTuc = (FrmGroupThoiTietTinTuc) FrmGroupThoiTietTinTuc.instance();
            frmGroupThoiTietTinTuc.getMultiTab().setTab(2, backTab, parameters);
          }
          return;
        }
      }
      if (type.equals("listUsers")) {
        back();
        clearCategory();
      }
      if (type.equals("phase")) {
        if (parentGroup instanceof FrmGroupBongDaXoSo) {
          FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
          frmGroupBongDaXoSo.getMultiTab().setTab(2, backTab, parameters);
        } else if (parentGroup instanceof  FrmGroupThoiTietTinTuc) {
          FrmGroupThoiTietTinTuc frmGroupThoiTietTinTuc = (FrmGroupThoiTietTinTuc) FrmGroupThoiTietTinTuc.instance();
          frmGroupThoiTietTinTuc.getMultiTab().setTab(2, backTab, parameters);
        }
        return;
      }
      break;
    default:
      txtMoney.keyReleased1(keyCode);
      break;
    }
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (alertId == 100) {
      if (eventType == GameAlert.YES_BUTTON) {
        Global.nextState(FrmUserInfo.instance(), null);
      }
    }
    if (alertId == 101 && eventType == GameAlert.YES_BUTTON) {
      if (parentGroup != null) {
        FrmGroupNapXuAndExchangeGold.backForm = parentGroup;
      } else {
        FrmGroupNapXuAndExchangeGold.backForm = this;
      }
      Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), parameters);
    }
    if (alertId == -1) {
      if (currentAction > 1) {
        clearCategory();
        back();
      }
    }
  }

  private boolean isInvalidUserMoney(int fee) {
    if (Global.currentUser.getMoney() < fee) {
      Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create2( "Bạn không đủ $ để sử dụng", "dịch vụ này", "Bạn có muốn nạp $ không?" ), 101);
      return true;
    }
    return false;
  }

  private boolean validateUserInfo() {
    User user = Global.currentUser;
    boolean isOK = true;
    if (user.getMobile().equals("") || user.getAddress().equals("") || user.getFullName().equals("")) {
      isOK = false;
    }
    return isOK;
  }

  private void back() {
    dy = DrawUtils.getTopBarHeight();
    currentAction--;
    String values = (String) logAction.get(String.valueOf(currentAction));
    StringArray requestParams = GameStringUtils.split(values, ",");
    int request = NumberUtil.parseInt(requestParams.get(0));
    Vector value = new Vector();
    for (int i = 1; i < requestParams.len(); i++) {
      value.addElement(requestParams.get(i));
    }
    request(request, value);
  }

  private void request(int header, Vector parameters1) {
    DataPackage dataPackage = DataPackage.createDataPackage1(header);
    for (int i = 0; i < parameters1.size(); i++) {
      dataPackage.putString(parameters1.elementAt(i).toString());
    }
    Global.appClient.write(dataPackage);
    logAction.put(String.valueOf(currentAction), buildLogAction(header, parameters1));
  }

  private String buildLogAction(int requestType, Vector parameters1) {
    StringBuffer sb = new StringBuffer();
    sb.append(String.valueOf(requestType));
    sb.append(",");
    for (int i = 0; i < parameters1.size(); i++) {
      sb.append(parameters1.elementAt(i).toString());
      sb.append(",");
    }
    if (sb.toString().endsWith(",")) {
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
  }

  private void clearCategory() {
    categories.removeAllElements();
    categoryIndex = 0;
    TEMP_Y = 0;
  }

  public void doTask() {
    if (type.equals("voting")) {
      if (seconds > 0) {
        seconds--;
      }
      if (seconds == 0) {
        minutes--;
        seconds = 60 * 10;
      }
      if (minutes == 0) {
        hours--;
        minutes = 60;
      }
      txtHours.setText(String.valueOf(hours));
      txtMinutes.setText(String.valueOf(minutes / 10));
      txtSeconds.setText(String.valueOf(seconds / 10));
    }
  }

  public void destroy() {
    text8 = null;
    text11 = null;
    text13 = null;
    categories = null;
    type = null;
    logAction = null;
    scrollBar = null;
    textView = null;
    txtHours = null;
    txtMinutes = null;
    txtSeconds = null;
    txtMoney = null;
    parameters = null;
    TEMP_Y = 0;
    betButtonSprite = null;
    buttonImage = null;
    isRequestVotingObject = false;
    removeTimerListener();
  }
}
